<!-- Your component -->
<template>
    <div class="login">
      <el-card class="box-card">
        <div class="clearfix">
          <slot name="header">
            <b>用户登录</b>
          </slot>
        </div>
        <el-form :model="user" label-width="10px" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="user.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="verifyCode">
            <el-input v-model="user.verifyCode" placeholder="验证码不能为空" style="width: 240px"></el-input>
            <img :src="verifyCode" alt="验证码" @click="refreshCode" style="vertical-align: middle;margin-left: 10px;">
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="submit('loginForm')" style="width: 100%;">确定</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        verifyCode: "",
        user: {
          username: "",
          password: "",
          verifyCode: ""
        },
        rules: {
          username: [{ required: true, message: "用户名不能为空", trigger: 'blur' }],
          password: [{ required: true, message: "密码不能为空", trigger: 'blur' }],
          verifyCode: [{ required: true, message: "验证码不能为空", trigger: 'blur' }]
        }
      };
    },
    mounted() {
      this.verifyCode = `http://localhost:8888/verifyCode?time=${new Date().getTime()}`;
    },
    methods: {
        submit(refName) {
  this.$refs[refName].validate((valid) => {
    if (valid) {
      this.$postRequest("http://localhost:8888/login", this.user)
        .then(resp => {
          if (resp && resp.code === 200) {
            // 将 data 保存到 Access-Token
            const accessToken = resp.data;
            sessionStorage.setItem("Access-Token", accessToken);

            // 登录成功，跳转到 /main 页面
            this.$router.replace("/main").catch(err => {
              console.error('路由跳转失败:', err);
            });
          } else {
            // 处理其他 code 的情况
            this.$message.error(resp.message || '登录失败');
          }
        })
        .catch(error => {
          console.error('登录失败:', error);
          this.$message.error('登录失败，请检查网络或服务器状态');
        });
    } else {
      this.$message.error('请填写完整的登录信息');
      return false;
    }
  });
}
,
        refreshCode() {
    this.verifyCode = `http://localhost:8888/verifyCode?time=${new Date().getTime()}`;
  }
    }
  };
  </script>
  
  <style>
  .login {
    display: flex;
    height: 100vh;
    justify-content: center;
    align-items: center;
    background-image: url("../assets/bg.jpg");
    overflow: hidden;
  }
  
  .box-card {
    width: 400px;
    height: 363px;
    border-radius: 6px;
  }
  
  .clearfix {
    text-align: center;
    font-size: 24px;
  }
  
  .el-form-item__content {
    display: flex;
    align-items: center;
  }
  </style>
  